package com.jimmyhsu.ecnudaowei.coursetable.view

import android.app.AlertDialog
import android.content.Context
import android.support.v4.content.ContextCompat
import com.aigestudio.wheelpicker.WheelPicker
import com.jimmyhsu.ecnudaowei.R

/**
 * Created by xuyanzhe on 2017/6/4.
 */
class WheelPickerWindowBuilder(context: Context): AlertDialog.Builder(context) {

    private var data: List<Int>? = null
    private val picker = WheelPicker(context)
    private var listener: ((pos :Int, data: Any)->Unit)? = null
    constructor(context: Context, begin: Int, end: Int, title: String) : this(context) {
        val data = (begin..end).toList()
        picker.data = data
        picker.visibleItemCount = 5
        picker.selectedItemTextColor = ContextCompat.getColor(context, R.color.colorAccent)
        setTitle(title)
        setView(picker)
        setNegativeButton("取消", null)
        setPositiveButton("确定", { _, _ ->
            listener?.invoke(picker.currentItemPosition, data[picker.currentItemPosition])
        })
    }

    fun setCurrentItem(pos: Int): WheelPickerWindowBuilder {
        picker.selectedItemPosition = pos
        return this
    }

    fun setOnDataChosenListener(l: ((pos :Int, data: Any)->Unit)): WheelPickerWindowBuilder {
        listener = l
        return this
    }

    fun createDialog(): AlertDialog = this.create()
}